<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">用户注册</div>
      <el-form ref="form" :model="form" :rules="formRules" label-width="0px" autocomplete="on" class="ms-content">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="username"
            tabindex="1"
          >
            <el-button slot="prepend" icon="el-icon-user" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="form.password"
            :type="passwordType"
            placeholder="password"
            autocomplete="on"
            tabindex="2"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleRegister"
          >
            <el-button slot="prepend" icon="el-icon-lock" />
          </el-input>
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="roles">
          <el-select v-model="form.roles" multiple placeholder="请选择角色" style="width:100%;">
            <el-option
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <div class="login-btn">
          <el-button type="primary" @click.native.prevent="handleRegister">注册</el-button>
        </div>
        <!-- <p class="login-tips">Tips : 用户名和密码随便填。</p> -->
      </el-form>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  name: 'Register',
  // components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
      // if (!validUsername(value)) {
      //   callback(new Error('Please enter the correct user name'))
      // } else {
      //   callback()
      // }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码最少6位'))
      } else {
        callback()
      }
    }
    const validateRoles = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('请选择角色'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        roles: []
      },
      roleOptions: [{
        label: 'admin',
        value: 'admin'
      },
      {
        label: 'editor',
        value: 'editor'
      }],
      formRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        roles: [{ required: true, type: 'array', trigger: 'blur', validator: validateRoles }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.form.username === '') {
      this.$refs.username.focus()
    } else if (this.form.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleRegister() {
      this.$refs.form.validate(valid => {
        console.log(valid)
        if (valid) {
          this.loading = true
          register(this.form).then(response => {
            this.$message(response.message)
            this.loading = false
            this.$router.push({ path: this.redirect || '/login' })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

$dark_gray:#889aa4;

.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #424242;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}

</style>
